﻿define(function () {

    return function ($) {
        /*******************************************************************************************/
        // jquery.jpager.js - version 0.1
        // A jQuery plugin for ajax pager
        // 
        // Copyright (c) 2012, Cheung (http://blog.xcai.com/javascript/jquery-plugin-jpager)
        // Licensed under the MIT License (MIT-LICENSE.txt)
        // http://www.opensource.org/licenses/mit-license.php
        // Created: 2012-10-18
        //
        /*******************************************************************************************/

        $.fn.jpager = function (options) {

            // Setup default option values
            var defaults = {
                totalItems: 88, // 总记录数，从后台获取
                limit: 10, // 每页显示的数据条数
                links: 10, // 显示页码的个数
                startPage: 1,
                showFirstLast: false, // 是否显示最后一页和第一页
                pageGo: function (page, offset, limit) { }
            };

            var options = $.extend(defaults, options);
            // Construct the nav bar
            var firstHtml = !options.showFirstLast ? '' : '<li><a href="#" class="first">|&lt;</a></li>';
            var lastHtml = !options.showFirstLast ? '' : '<li><a href="#" class="last">&gt;|</a></li>';
            var prevHtml = '<li><a href="#" class="prev">&lt;</a></li>';
            var nextHtml = '<li><a href="#" class="next">&gt;</a></li>';

            return this.each(function () {
                // Calculate the number of pages needed
                var totalPages = Math.ceil(options.totalItems / options.limit);
                if (options.startPage < 1 || options.startPage > totalPages) {
                    options.startPage = 1;
                }
                var $page_container = $(this);
                renderPages($page_container, totalPages, options.startPage);
            });

            // 生成显示的html代码
            function renderPages(container, totalPages, pageIndex) {
                var pagesHtml = "<ul>";
                var pages = currentPages(totalPages, pageIndex);

                if (pages.length > 1) {
                    if (pageIndex > 1) {
                        pagesHtml += firstHtml;
                        pagesHtml += prevHtml;
                    }
                    for (var i = 0; i < pages.length; i++) {
                        if (pageIndex == pages[i]) {
                            pagesHtml += '<li class="active"><a href="#" data-page="' + pages[i] + '">' + pages[i] + '</a></li>';
                        } else {
                            pagesHtml += '<li><a href="#" data-page="' + pages[i] + '">' + pages[i] + '</a></li>';
                        }
                    }
                    if (pageIndex < totalPages) {
                        pagesHtml += nextHtml;
                        pagesHtml += lastHtml;
                    }
                }
                pagesHtml += "</ul>";
                container.empty();
                container.html(pagesHtml);

                var first = container.find('.first');
                var last = container.find('.last');
                var prev = container.find('.prev');
                var next = container.find('.next');

                first.attr("data-page", 1);
                last.attr("data-page", pages[pages.length - 1]);
                prev.attr("data-page", pageIndex - 1);
                next.attr("data-page", pageIndex + 1);

                container.find("a").each(function () {
                    var pageLink = $(this);
                    var goIdx = parseInt(pageLink.attr("data-page"));
                    pageLink.click(function () {
                        if (options.pageGo) {
                            options.pageGo(goIdx, (goIdx - 1) * options.limit, options.limit);
                        }
                        renderPages(container, totalPages, goIdx);
                        return false;
                    });
                });
            }

            // 计算当前显示的页面，页码默认从1开始
            function currentPages(totalPages, pageIndex) {
                var links = options.links;
                var pages = new Array(totalPages < links ? totalPages : links); //[];

                if (totalPages > links) {
                    var idx = Math.ceil(links / 2);
                    if (pageIndex <= idx) {
                        for (var i = 0; i < links; i++) {
                            pages[i] = i + 1;
                        }
                    } else if (totalPages - pageIndex >= idx) {
                        for (var i = 0; i < links; i++) {
                            pages[i] = pageIndex + idx - links + i;
                        }
                    } else {
                        for (var i = 0; i < links; i++) {
                            pages[i] = totalPages - links + i + 1;
                        }
                    }
                } else {
                    for (var i = 0; i < totalPages; i++) {
                        pages[i] = i + 1;
                    }
                }
                return pages;
            }
        };

    };
});